package com.ruoyi.web.controller.project;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Product;
import com.ruoyi.system.service.IProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品Controller
 *
 * @author 萧萧
 * @date 2021-09-25
 */
@Controller
@RequestMapping("/system/product" )
public class ProductController extends BaseController {
    private String prefix = "system/product" ;

    @Autowired
    private IProductService productService;

    @RequiresPermissions("system:product:view" )
    @GetMapping()
    public String product() {
        return prefix + "/product" ;
    }

    /**
     * 查询产品列表
     */
    @RequiresPermissions("system:product:list" )
    @PostMapping("/list" )
    @ResponseBody
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("system:product:export" )
    @Log(title = "产品" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    @ResponseBody
    public AjaxResult export(Product product) {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        return util.exportExcel(list, "产品数据" );
    }

    /**
     * 新增产品
     */
    @GetMapping("/add" )
    public String add() {
        return prefix + "/add" ;
    }

    /**
     * 新增保存产品
     */
    @RequiresPermissions("system:product:add" )
    @Log(title = "产品" , businessType = BusinessType.INSERT)
    @PostMapping("/add" )
    @ResponseBody
    public AjaxResult addSave(Product product) {
        SysUser sysUser = ShiroUtils.getSysUser();
        product.setCreateBy(sysUser.getUserName());
        product.setCreateTime(DateUtils.getNowDate());
        return toAjax(productService.save(product));
    }

    /**
     * 修改产品
     */
    @GetMapping("/edit/{id}" )
    public String edit(@PathVariable("id" ) Integer id, ModelMap mmap) {
        Product product = productService.getById(id);
        mmap.put("product" , product);
        return prefix + "/edit" ;
    }

    /**
     * 修改保存产品
     */
    @RequiresPermissions("system:product:edit" )
    @Log(title = "产品" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit" )
    @ResponseBody
    public AjaxResult editSave(Product product) {
        return toAjax(productService.updateById(product));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("system:product:remove" )
    @Log(title = "产品" , businessType = BusinessType.DELETE)
    @PostMapping("/remove" )
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productService.removeByIds(Arrays.asList(ids.split("," ))));
    }
}
